package com.example.springsecurity.controller;

import com.alibaba.fastjson2.JSON;
import com.example.springsecurity.common.annotation.SystemLogInterface;
import com.example.springsecurity.common.config.UserDetailsServiceImpl;
import com.example.springsecurity.common.enums.BusinessTypeEnum;
import com.example.springsecurity.common.exception.BizException;
import com.example.springsecurity.common.resp.ResponseResult;
import com.example.springsecurity.entity.SystemUser;
import com.example.springsecurity.service.SystemLoginService;
import com.example.springsecurity.service.SystemMenuService;
import com.example.springsecurity.common.vo.UserLoginResultVo;
import com.example.springsecurity.common.vo.login.SystemMenuTreeVo;
import com.example.springsecurity.common.vo.login.SystemUserLoginInfoVo;
import com.xingyuv.captcha.model.common.ResponseModel;
import com.xingyuv.captcha.model.vo.CaptchaVO;
import com.xingyuv.captcha.service.CaptchaService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.example.springsecurity.common.CommonMethod.getRemoteId;
import static com.example.springsecurity.common.resp.ResponseResult.success;

/**
 * @Description：系统登陆控制层
 * @Author： fengcheng
 * @Date： 2024/12/18 15:21
 */
@Slf4j
@Data
@RestController
@Tag(name = "登陆管理模块")
@RequestMapping("/system/login")
public class SystemLoginController {

    @Resource
    private CaptchaService captchaService;

    @Resource
    private SystemLoginService systemLoginService;

    @Resource
    private SystemMenuService systemMenuService;

    @Resource
    private UserDetailsServiceImpl userDetailsService;

    /**
     * @Description：登陆系统
     * @Author：fengcheng
     * @Date：2025/1/17 09:54
     * @Param：systemUser
     * @ParamsType：[com.example.springsecurity.entity.SystemUser]
     * @Return：com.example.springsecurity.common.response.ResponseResult<com.example.springsecurity.vo.UserLoginResultVo>
     */
    @Operation(summary = "系统登陆")
    @PostMapping("/login")
    public ResponseResult<UserLoginResultVo> login(@RequestBody SystemUser systemUser) {
        if (ObjectUtils.isEmpty(systemUser) || StringUtils.isBlank(systemUser.getMobile()) || StringUtils.isBlank(systemUser.getPassword()))
            throw new BizException("手机号或密码不能为空！");
        log.info("用户登陆成功，登陆账号：{}", systemUser.getMobile());
        return ResponseResult.success(systemLoginService.login(systemUser));
    }

    /**
     * @Description：退出系统
     * @Author：fengcheng
     * @Date：2025/1/17 09:54
     * @Param：systemUser
     * @ParamsType：[com.example.springsecurity.entity.SystemUser]
     * @Return：com.example.springsecurity.common.response.ResponseResult<java.lang.Boolean>
     */
    @Operation(summary = "系统登出")
    @PostMapping("/logout")
    public ResponseResult<Boolean> logout(@RequestHeader("Authorization") String authorizationHeader) {
        return ResponseResult.success(systemLoginService.logout(authorizationHeader));
    }

    /**
     * @Description：登陆时是否显示验证码
     * @Author：fengcheng
     * @Date：2025/1/17 09:54
     * @Param：mobile
     * @ParamsType：[java.lang.String]
     * @Return：com.example.springsecurity.common.response.ResponseResult<java.lang.Boolean>
     */
    @Operation(summary = "是否显示验证码")
    @PostMapping(value = "/enable")
    public ResponseResult<Boolean> enableCaptcha(String mobile) {
        return success(systemLoginService.enableCaptcha(mobile));
    }

    /**
     * @Description：获取图形验证码
     * @Author：fengcheng
     * @Date：2025/1/17 09:52
     * @Param：data
     * @Param：request
     * @ParamsType：[com.xingyuv.captcha.model.vo.CaptchaVO, jakarta.servlet.http.HttpServletRequest]
     * @Return：com.example.springsecurity.common.response.ResponseResult<java.lang.Object>
     */
    @Operation(summary = "获取图形验证码")
    @PostMapping({"/get"})
    public ResponseResult<Object> get(@RequestBody CaptchaVO data, HttpServletRequest request) {
        assert request.getRemoteHost() != null;
        data.setBrowserInfo(getRemoteId(request));
        ResponseModel responseModel = captchaService.get(data);
        return success(responseModel.getRepData());
    }

    /**
     * @Description：校验图形验证码
     * @Author：fengcheng
     * @Date：2025/1/17 09:51
     * @Param：data
     * @Param：request
     * @ParamsType：[com.xingyuv.captcha.model.vo.CaptchaVO, jakarta.servlet.http.HttpServletRequest]
     * @Return：com.example.springsecurity.common.response.ResponseResult<java.lang.Object>
     */
    @Operation(summary = "校验图形验证码")
    @PostMapping("/check")
    public ResponseResult<Object> check(@RequestBody CaptchaVO data, HttpServletRequest request) {
        data.setBrowserInfo(getRemoteId(request));
        ResponseModel check = captchaService.check(data);
        return success(check.getRepData());
    }

    /**
     * @Description：获取用户信息接口
     * @Author：fengcheng
     * @Date：2025/1/17 09:50
     * @Param：authorizationHeader
     * @ParamsType：[java.lang.String]
     * @Return：com.example.springsecurity.common.response.ResponseResult<com.example.springsecurity.vo.login.SystemUserLoginInfoVo>
     */
    @Operation(summary = "获取用户信息")
    @PostMapping(value = "/getUserInfo")
    @SystemLogInterface(title = "获取用户信息接口", businessType = BusinessTypeEnum.QUERY)
    public ResponseResult<SystemUserLoginInfoVo> getUserInfo(@RequestHeader("Authorization") String authorizationHeader) {
        SystemUserLoginInfoVo userInfo = systemLoginService.getUserInfo(authorizationHeader);
        log.info("获取用户信息成功，用户信息：{}", JSON.toJSONString(userInfo));
        return success(userInfo);
    }

    /**
     * @Description：用户登陆成功后，获取系统菜单信息
     * @Author：fengcheng
     * @Date：2025/1/17 09:50
     * @ParamsType：[]
     * @Return：com.example.springsecurity.common.response.ResponseResult<java.util.List<com.example.springsecurity.vo.login.SystemMenuTreeVo>>
     */
    @Operation(summary = "获取菜单信息")
    @PostMapping(value = "/getMenuList")
    // @PreAuthorize("@pre.hasPermission('sys:menu:list')")
    @SystemLogInterface(title = "获取获取菜单信息接口", businessType = BusinessTypeEnum.QUERY)
    public ResponseResult<List<SystemMenuTreeVo>> getMenuList() {
        return success(systemMenuService.getMenuList());
    }
}
